package com.ruoyi.web.controller.gxs;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gxs.domain.GxsStudyChange;
import com.ruoyi.gxs.service.IGxsStudyChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学习成绩变化Controller
 * 
 * @author 高晓松
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/gxs/change")
public class GxsStudyChangeController extends BaseController
{
    @Autowired
    private IGxsStudyChangeService gxsStudyChangeService;

    /**
     * 查询学习成绩变化列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxsStudyChange gxsStudyChange)
    {
        startPage();
        List<GxsStudyChange> list = gxsStudyChangeService.selectGxsStudyChangeList(gxsStudyChange);
        return getDataTable(list);
    }

    /**
     * 导出学习成绩变化列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:export')")
    @Log(title = "学习成绩变化", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsStudyChange gxsStudyChange)
    {
        List<GxsStudyChange> list = gxsStudyChangeService.selectGxsStudyChangeList(gxsStudyChange);
        ExcelUtil<GxsStudyChange> util = new ExcelUtil<GxsStudyChange>(GxsStudyChange.class);
        util.exportExcel(response, list, "学习成绩变化数据");
    }

    /**
     * 获取学习成绩变化详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:query')")
    @GetMapping(value = "/{gxsId}")
    public AjaxResult getInfo(@PathVariable("gxsId") Long gxsId)
    {
        return success(gxsStudyChangeService.selectGxsStudyChangeByGxsId(gxsId));
    }

    /**
     * 新增学习成绩变化
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:add')")
    @Log(title = "学习成绩变化", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsStudyChange gxsStudyChange)
    {
        return toAjax(gxsStudyChangeService.insertGxsStudyChange(gxsStudyChange));
    }

    /**
     * 修改学习成绩变化
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:edit')")
    @Log(title = "学习成绩变化", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsStudyChange gxsStudyChange)
    {
        return toAjax(gxsStudyChangeService.updateGxsStudyChange(gxsStudyChange));
    }

    /**
     * 删除学习成绩变化
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:remove')")
    @Log(title = "学习成绩变化", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsIds}")
    public AjaxResult remove(@PathVariable Long[] gxsIds)
    {
        return toAjax(gxsStudyChangeService.deleteGxsStudyChangeByGxsIds(gxsIds));
    }


    /**
     * 我的学习成绩变化列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:change:list')")
    @GetMapping("/MyList")
    public TableDataInfo MyList(GxsStudyChange gxsStudyChange)
    {
        startPage();
        List<GxsStudyChange> list = gxsStudyChangeService.selectGxsStudyChangeList(gxsStudyChange);

        String username = getUsername();

        for (int i = 0 ; i < list.size(); i++){
            String userName = list.get(i).getUserName();
            if (!userName.equals(username)) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }
}
